package kevin.ecstorm.pdd.rest;

import com.alibaba.fastjson.JSON;
import com.pdd.pop.sdk.http.PopBaseHttpRequest;
import kevin.ecstorm.pdd.config.PddAppAuth;
import kevin.ecstorm.pdd.service.IPddService;
import kevin.framework.core.exception.BusinessException;
import kevin.framework.core.web.JsonResponse;
import kevin.framework.core.web.RestDoing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ©Copyright ©1968-2020 Midea Group,IT
 * FileName: PddRest
 * Author: kevin
 * Date: 2020年03月08日 23:40:29
 * Description: PddRest服务接口
 */
@RestController
@RequestMapping("/pdd")
public class PddRest {
    @Autowired
    private IPddService pddService;
    @Autowired
    private PddAppAuth auth;
    private static final String REQUEST_FREFIX = "com.pdd.pop.sdk.http.api.request.";
    private static final String REQUEST_SUFFIX = "Request";

    @PostMapping("/re")
    public JsonResponse syncInvoke(@RequestBody String requestParam, @RequestParam("router") String router) {
        RestDoing restDoing = jsonResponse -> {
            String[] interfs = router.split("\\.");
            StringBuilder sb = new StringBuilder();
            for (String str : interfs) {
                char[] chars = str.toCharArray();
                if (chars[0] >= 'a' && chars[0] <= 'z') {
                    chars[0] = (char) (chars[0] - 32);
                }
                sb.append(chars);
            }
            String interfName = sb.toString();
            String requestClassName = REQUEST_FREFIX + interfName + REQUEST_SUFFIX;
            try {
                Class<?> requestClss = Class.forName(requestClassName);
                PopBaseHttpRequest param = (PopBaseHttpRequest) JSON.parseObject(requestParam, requestClss);
                jsonResponse.setData(pddService.syncInvoke(param, auth));
            } catch (ClassNotFoundException e) {
                throw new BusinessException("1");
            }
        };
        return restDoing.go();
    }


}
